package match.chuanchao;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import match.chuanchao.dto.AddOrderReq;
import match.chuanchao.dto.AddOrderResp;
import match.chuanchao.dto.GuanSaiRenInfoResp;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author xiang.liao
 * @create 2025/11/3
 */
public class Chuanchao2 {

    public static String guansairen = "https://client.maitix.com/api/user/getBearers?pageIndex=1&pageSize=20&needCount=true&langType=1";
    // 下单页面，这里要返回订单号
    public static String addOrder = "https://client.maitix.com/api/trade/submitGroupedCart";
    public static String eventId = "252324018";
    public static String priceId = "252324018";
    public static String projectId = "233988009";
    private static BigDecimal perMoney = BigDecimal.valueOf(5.00D);

    private static String header = ":authority\n" + "client.maitix.com\n" + ":method\n" + "GET\n" + ":path\n" + "/api/user/userInfo?langType=1\n" + ":scheme\n" + "https\n" + "_r\n" + "21\n" + "Accept\n" + "*/*\n" + "Accept-Encoding\n" + "gzip, deflate, br, zstd\n" + "Accept-Language\n" + "zh-CN,zh;q=0.9\n" + "Coeus\n" + "10086\n" + "Cookie\n" + "cna=mrWAIeq+nAcCAbfeylSzsS/q; MZOPERATEID=NDkxNjA1NTA; MZCONSUMERJSESSIONID=fc3bf864c543463b84670b15ad23a580; xlly_s=1; JSESSIONID=C4C1CF00D755BC1FFF46F00D44C52922; isg=BLe3WC7HkoYClxf7PgnYNBWWRqsBfIvev30-sAlk0gbtuNX6EU1sLmranh7mUGNW; tfstk=g05rALaY27Fr5MobNidE7Md2b8RJsBr6t6tB-wbHP3xkVuneYgIpKLwRFeVFyn1WVaOk0i71JTTQqzBe-G_Bqa_QvXuHws6SO3ZJTwj6TlZ_15_Rypd3fl_MQiGw_enoEDtkiQLQPKfVw5_Ryq0rxP601WuRIdKHxMvkoxYMmbchxbx0ueYIqvclxZ42DeonKeYkinYeSXAHtMb0ueKDrBvlxZ42JnxnI351_x8WErT3AO4qV5dJjaxqtfuezLxa6nhntsRRuhboBXchgUJliWo5nTJ5KNpONsVEMQ_NnIY1y7l2saYNVnsgsjRclZjBBtEK1QsVTd5Xe2qpEgWc59v4r7b2a3J1aigi3QWh26JRU2UOoQjABhpuHoLVN19eXKo0qZ_2qdWFVohHVO7c0d1jcfOdD9jH8imP4GopoIUc9TonLLYvuha4uEPX4x-9m6nrpvpktE-_onDKpLYvuha4uvHpEBY2frxc.\n" + "Epeius\n" + "1762154258604\n" + "Hecate\n" + "3102d69e9e10bb6b3652631c10e1b24c\n" + "Mzoperateid\n" + "NDkxNjA1NTA\n" + "Origin\n" + "https://scgchc.maitix.com\n" + "Priority\n" + "u=1, i\n" + "Referer\n" + "https://scgchc.maitix.com/\n" + "Sec-Ch-Ua\n" + "\"Google Chrome\";v=\"141\", \"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"141\"\n" + "Sec-Ch-Ua-Mobile\n" + "?1\n" + "Sec-Ch-Ua-Platform\n" + "\"Android\"\n" + "Sec-Fetch-Dest\n" + "empty\n" + "Sec-Fetch-Mode\n" + "cors\n" + "Sec-Fetch-Site\n" + "same-site\n" + "Site\n" + "m\n" + "User-Agent\n" + "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36\n" + "X-Mz-Session\n" + "fc3bf864c543463b84670b15ad23a580\n" + "X-Xsrf-Token\n" + "bfaad446531e43b198620ccb30271632";

    public static void main(String[] args) {
        List<GuanSaiRenInfoResp.DataList> dataList = null;

        // 观赛人信息
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet httpPost = new HttpGet(guansairen);
            // header
            String[] split = header.split("\n");
            for (int i = 0; i < split.length; i++, i++) {
                if (split[i].equalsIgnoreCase("Epeius")) {
                    httpPost.setHeader(split[i], String.valueOf(System.currentTimeMillis()));
                } else {
                    httpPost.setHeader(split[i], split[i + 1]);
                }
            }

            try (CloseableHttpResponse response = client.execute(httpPost)) {
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String result = EntityUtils.toString(responseEntity);
                    GuanSaiRenInfoResp guanSaiRenInfoResp = JSONObject.parseObject(result, GuanSaiRenInfoResp.class);
                    System.out.println(JSONObject.toJSONString(guanSaiRenInfoResp));
                    if (guanSaiRenInfoResp != null && guanSaiRenInfoResp.getData() != null && CollectionUtil.isNotEmpty(guanSaiRenInfoResp.getData().getDataList())) {
                        dataList = guanSaiRenInfoResp.getData().getDataList();
                    }
                }
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (CollectionUtil.isEmpty(dataList)) {
            System.out.println("没有获取到观赛人");
            return;
        }

        // 下单
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(addOrder);
            // header
            String[] split = header.split("\n");
            for (int i = 0; i < split.length; i++, i++) {
                if (split[i].equalsIgnoreCase("Epeius")) {
                    httpPost.setHeader(split[i], String.valueOf(System.currentTimeMillis()));
                } else {
                    httpPost.setHeader(split[i], split[i + 1]);
                }
            }
            // body
            AddOrderReq addOrderReq = new AddOrderReq();
            addOrderReq.setProjectId(projectId);

            AddOrderReq.CheckGoods checkGoods = new AddOrderReq.CheckGoods();
            checkGoods.setEventId(eventId);
            addOrderReq.getCheckGoods().add(checkGoods);

            int bearCount = Math.min(dataList.size(), 4);
            for (int i = 0; i < bearCount; i++) {
                AddOrderReq.TicketRestrict ticketRestrict = new AddOrderReq.TicketRestrict();
                ticketRestrict.setPriceId(priceId);
                ticketRestrict.setBearerId(dataList.get(i).getBearerId());
                addOrderReq.getTicketRestricts().add(ticketRestrict);
            }
            String money = BigDecimal.valueOf(bearCount).multiply(perMoney).setScale(2, RoundingMode.HALF_UP).toPlainString();
            addOrderReq.setReceivableMoney(money);

            StringEntity entity = new StringEntity(JSONObject.toJSONString(addOrderReq));
            httpPost.setEntity(entity);

            try (CloseableHttpResponse response = client.execute(httpPost)) {
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String result = EntityUtils.toString(responseEntity);
                    System.out.println(result);
//                    AddOrderResp addOrderResp = JSONObject.parseObject(result, AddOrderResp.class);
//                    System.out.println(JSONObject.toJSONString(addOrderResp));
                }
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
